<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ServiceController
 *
 * @author Vu
 */
include APPLICATION_PATH."/models/Area_Agent_Product.php";
include APPLICATION_PATH."/models/Agent.php";
class Admin_ServiceController extends Zend_Controller_Action {
    //put your code here
    public function init() {
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->Agentmodel= new Model_Agent();
        $this->AgentmodelMapper= new Model_AgentMapper();
        $this->AreaAgentProductmodel= new Model_Area_Agent_Product();
        $this->AreaAgentProductmodelMapper= new Model_Area_Agent_ProductMapper();
    }
     public function indexAction(){
         if ($this->_request->isPost()) {
            $action = $_POST['action'];
        } else {
            $action = '';
        }
         switch ($action) {
                case 'getagentreport4':
                   $this->_helper->layout()->disableLayout();
                   $agentHTML = '<select name="agent_id" style="width: 386px;"><option value=""></option></select>';
                   if ($this->_request->isPost()) {
                       $nam = $this->_getParam("nam", "");
                       if (is_numeric($nam)) {
                           $agentHTML = GlobalLib::getComboMulti("agent_id", "agent", "id", "name","address","",true,"where id IN (SELECT DISTINCT agent.id
                                                                                                                                                    FROM  `order` 
                                                                                                                                                    INNER JOIN  `order_detail` ON order.order_no = order_detail.order_id
                                                                                                                                                    INNER JOIN  `agent` ON order.store_id = agent.id
                                                                                                                                                    WHERE order_detail.order_status_id = 2 AND YEAR( order_detail.order_created ) =".$nam.")");
                       }
                   }
                   echo '[{"html":\'' . $agentHTML . '\'}]';
                   exit;
                   break;
                case 'getagentofprovince':
                   $this->_helper->layout()->disableLayout();
                   $agentHTML = '<select name="agent_id" style="width: 386px;"><option value=""></option></select>';
                   $storeHTML = '<select name="store_id" style="width: 386px;"><option value=""></option></select>';
                   if ($this->_request->isPost()) {
                       $province_id = $this->_getParam("province_id", "");
                       if (is_numeric($province_id)) {
                           $agentHTML = GlobalLib::getComboMulti('agent_id', 'agent', 'id', 'name','address', 0, true, 'where province_id in (select id from province where id=' . $province_id . ')');

                       }
                   }
                   echo '[{"html":\'' . $agentHTML . '\'}]';
                   exit;
                   break;
               case 'getagent':
                $this->_helper->layout()->disableLayout();
                $agentHTML = '<select name="agent_id" style="width: 386px;"><option value=""></option></select>';
                $storeHTML = '<select name="store_id" style="width: 386px;"><option value=""></option></select>';
                if ($this->_request->isPost()) {
                    $area_id = $this->_getParam("area_id", "");
                    if (is_numeric($area_id)) {
                        $agentHTML = GlobalLib::getCombo('agent_id', 'agent', 'id', 'name', 0, false, 'where district_id in (select id from district where id=' . $area_id . ')');
                       
                    }
                }
                echo '[{"html":\'' . $agentHTML . '\'}]';
                exit;
                break;
            case 'getproduct':
                 $this->_helper->layout()->disableLayout();
                $factorHTML = '<input id="factor" class="form-control" name="factor" value="2.35" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();">';
                $quantityHTML = '<input id="quantity" class="form-control" name="quantity" value="2.35" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();">';
                $chargesHTML = '<input id="charges" class="form-control" name="charges" value="2.35" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();">';
                if ($this->_request->isPost()) {
                    $agent_id = $this->_getParam("agent_id", "");
                    $product_id = $this->getParam("product_id", "");
                    $date = $this->getParam("order_created", "");
                    $this->AgentmodelMapper->find($agent_id, $this->Agentmodel);
                    $province_id = $this->Agentmodel->getProvince_Id();
                    $district_id = $this->Agentmodel->getDistrict_Id();
                    $area_id=$this->Agentmodel->getArea_Id();
                    $getdate = strtotime(GlobalLib::toMysqlDateString($date));
                    if ($this->AreaAgentProductmodelMapper->getFactor($area_id, $agent_id, $product_id, $province_id, $district_id, $getdate) == 0) {
                        $factorHTML = '<input id="factor" class="form-control" name="factor" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();" readonly="true">';
                        $quantityHTML = '<input id="quantity" class="form-control" name="quantity" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();">';
                        $chargesHTML = '<input id="charges" class="form-control" name="charges" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();" readonly="true">';
                        echo '[{"html1":\'' . $factorHTML . '\',"html2":\'' . $quantityHTML . '\',"html3":\'' . $chargesHTML . '\'}]';
                        exit;
                    }
                    foreach ($this->AreaAgentProductmodelMapper->getFactor($area_id, $agent_id, $product_id, $province_id, $district_id, $getdate) as $items) {
                        $factorHTML = '<input id="factor" class="form-control" name="factor" value="' . $items->getFactor() . '" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();" readonly="true">';
                        $quantityHTML = '<input id="quantity" class="form-control" name="quantity" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();">';
                        $chargesHTML = '<input id="charges" class="form-control" name="charges" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();" readonly="true">';
                        echo '[{"html1":\'' . $factorHTML . '\',"html2":\'' . $quantityHTML . '\',"html3":\'' . $chargesHTML . '\'}]';
                        exit;
                    }
                }
                    break;
            case 'getfactor':
                $this->_helper->layout()->disableLayout();
                $factorHTML = '<input id="factor" class="form-control" name="factor" value="2.35" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();">';
                $quantityHTML = '<input id="quantity" class="form-control" name="quantity" value="2.35" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();">';
                $chargesHTML = '<input id="charges" class="form-control" name="charges" value="2.35" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();">';
                if ($this->_request->isPost()) {
                    $agent_id = $this->_getParam("agent_id", "");
                    $product_id = $this->getParam("product_id", "");
                    $date = $this->getParam("date_id", "");
                    $this->AgentmodelMapper->find($agent_id, $this->Agentmodel);
                    $province_id = $this->Agentmodel->getProvince_Id();
                    $district_id = $this->Agentmodel->getDistrict_Id();
                    $area_id=$this->Agentmodel->getArea_Id();
                    $getdate = strtotime(GlobalLib::toMysqlDateString($date));
                    if ($this->AreaAgentProductmodelMapper->getFactor($area_id, $agent_id, $product_id, $province_id, $district_id, $getdate) == 0) {
                        $factorHTML = '<input id="factor" class="form-control" name="factor" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();" readonly="true">';
                        $quantityHTML = '<input id="quantity" class="form-control" name="quantity" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();">';
                        $chargesHTML = '<input id="charges" class="form-control" name="charges" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();" readonly="true">';
                        echo '[{"html":\'' . $factorHTML . '\',"html1":\'' . $quantityHTML . '\',"html2":\'' . $chargesHTML . '\'}]';
                        exit;
                    }
                    foreach ($this->AreaAgentProductmodelMapper->getFactor($area_id, $agent_id, $product_id, $province_id, $district_id, $getdate) as $items) {
                        $factorHTML = '<input id="factor" class="form-control" name="factor" value="' . $items->getFactor() . '" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();" readonly="true">';
                        $quantityHTML = '<input id="quantity" class="form-control" name="quantity" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();">';
                        $chargesHTML = '<input id="charges" class="form-control" name="charges" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();" readonly="true">';
                        echo '[{"html":\'' . $factorHTML . '\',"html1":\'' . $quantityHTML . '\',"html2":\'' . $chargesHTML . '\'}]';
                        exit;
                    }
                }
                break;  
            case 'getprovince':
                $this->_helper->layout()->disableLayout();
                $provinceHTML = '<select name="province_id" style="width: 386px;"><option value=""></option></select>';
                if ($this->_request->isPost()) {
                    $area_id = $this->_getParam("area_id", "");
                    if (is_numeric($area_id)) {
                        $provinceHTML = GlobalLib::getCombo('province_id', 'province', 'id', 'name', 0, false, 'where id in (select province_id from area_province_category where area_id=' . $area_id.')');
                    }
                }
                echo '[{"html":\'' . $provinceHTML . '\'}]';
                exit;
                break;
            case 'getdistrict':
                $this->_helper->layout()->disableLayout();
                $productHTML = '<select name="agent_id" style="width: 386px;"><option value=""></option></select>';
                $factorHTML = '<input id="factor" class="form-control" name="factor" value="0" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();" readonly="true">';
                $quantityHTML = '<input id="quantity" class="form-control" name="quantity" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();">';
                $chargesHTML = '<input id="charges" class="form-control" name="charges" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();" readonly="true">';
                $districtHTML = '<select name="dictrict_id" style="width: 386px;"><option value=""></option></select>';
                if ($this->_request->isPost()) {
                    $province_id = $this->_getParam("province_id", "");
                    if (is_numeric($province_id)) {
                        $districtHTML = GlobalLib::getCombo('dictrict_id', 'district', 'id', 'name', 0, false, 'where province_id=' . $province_id);
                        $productHTML = GlobalLib::getCombo('product_id', 'product', 'id', 'name', 0, false, '');
                    }
                }
                echo '[{"html":\'' . $districtHTML . '\',"html1":\'' . $productHTML . '\',"html2":\'' . $factorHTML . '\',"html3":\'' . $quantityHTML . '\',"html4":\'' . $chargesHTML . '\'}]';
                exit;
                break;
             case 'getagentbydistrict':
                $this->_helper->layout()->disableLayout();
                $agentHTML = '<select name="agent_id" style="width: 386px;"><option value=""></option></select>';
                 $productHTML = '<select name="agent_id" style="width: 386px;"><option value=""></option></select>';
                $factorHTML = '<input id="factor" class="form-control" name="factor" value="0" onChange="change_text();" onkeydown="change_text();" onkeyup="change_text();" readonly="true">';
                $quantityHTML = '<input id="quantity" class="form-control" name="quantity" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();">';
                $chargesHTML = '<input id="charges" class="form-control" name="charges" value="0" onkeydown="change_text();" onChange="change_text();" onkeyup="change_text();" readonly="true">';
                if ($this->_request->isPost()) {
                    $area_id = $this->_getParam("area_id", "");
                    if (is_numeric($area_id)) {
                        $agentHTML = GlobalLib::getCombo('agent_id', 'agent', 'id', 'name', 0, false, 'where district_id in (select id from district where id=' . $area_id . ')');
                        $productHTML = GlobalLib::getCombo('product_id', 'product', 'id', 'name', 0, false, '');
                    }
                }
                echo '[{"html":\'' . $agentHTML . '\',"html1":\'' . $productHTML . '\',"html2":\'' . $factorHTML . '\',"html3":\'' . $quantityHTML . '\',"html4":\'' . $chargesHTML . '\'}]';
                exit;
                break;
     }
}
}
